using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;

namespace DaphneTranslator
{
	public partial class CultureSelectDialog : Gtk.Dialog
	{	
		private List<CultureRecord> _lstCultureList;
		
		public CultureSelectDialog ()
		{
			this.Build ();
			
			buttonOk.Clicked += OkButtonHandleClicked;
			buttonCancel.Clicked += CancelButtonClicked;
		
			
			_lstCultureList = GetCultureList ();
			UpdateComboBox ();			
			
		}
		
		private void UpdateComboBox ()
		{
			int count = cbxCulture.Model.IterNChildren ();
			for (int i = 0; i < count; i++)
				cbxCulture.RemoveText (0);
			
			foreach (var item in _lstCultureList) {
				cbxCulture.InsertText (0, item.CultureName);
			}		
			
			cbxCulture.Active = 0;
		}
		
		public void SetCulturesToSelect (List<string> cultures)
		{
			_lstCultureList.Clear ();			
			
			foreach (string sCulture in cultures) {
				CultureRecord rec = new CultureRecord ();
				rec.CultureCode = sCulture;
				rec.CultureName = sCulture;
				
				_lstCultureList.Add (rec);
			}
					
			UpdateComboBox ();					
		}
		
		public void SetQuestionMessage (string sMessage)
		{
			this.Title = sMessage;
		}
		
		public void SetButtonCancelVisible (bool oEnable)
		{
			buttonCancel.Visible = oEnable;
		}

		void CancelButtonClicked (object sender, EventArgs e)
		{
			this.Respond (Gtk.ResponseType.Cancel);
			this.Hide ();		
		}

		void OkButtonHandleClicked (object sender, EventArgs e)
		{
			this.Respond (Gtk.ResponseType.Apply);
			this.Hide ();					
		}
		
		public string GetSelectedCulture ()
		{
			string sCultureName = cbxCulture.ActiveText;
			foreach (var item in _lstCultureList) {
				if (item.CultureName == sCultureName)
					return item.CultureCode;
			}
			
			return null;
		}	
		
		private List<CultureRecord> GetCultureList ()
		{
			List<CultureRecord> list = new List<CultureRecord> ();
			
			CultureInfo[] cultureInfoList = CultureInfo.GetCultures (CultureTypes.AllCultures);
			foreach (CultureInfo ci in cultureInfoList) {
				CultureRecord cr = new CultureRecord ();
				cr.CultureName = ci.DisplayName;
				cr.CultureCode = ci.Name;
				
				list.Add (cr);						
			}			
			
			return list;
		}						
		
		private class CultureRecord
		{
			public string CultureCode
			{ get; set; }
			
			public string CultureName
			{ get; set; }
			
			public override string ToString ()
			{
				return this.CultureName;
			}
		}
		
	}
}

